<?php
namespace app\admin\controller;

class Notice extends Common
{
    public function notice_list()
    {
        $ret = db('notice')->order('addtime desc')->paginate(3);
        $pageHtml = $ret->render();
        $this->assign('ret',$ret);
        $this->assign('pageHtml',$pageHtml);
        $this->assign('pick',5);
        return view('notice_list');
    }

    public function notice_del()
    {
        $id = input('get.id');
        $ret = db('notice')->where("id={$id}")->delete();
        if(!$ret)
        {
            return [
                'msg' => '删除失败',
                'status' => 0
            ];
        }
        return [
            'msg' => '删除成功',
            'status' => 1
        ];
    }

    public function add_notice(){
        $this->assign('pick',5);
        return view('add_notice');
    }

    public function add_new_action(){
        $title     = input('post.title');
        $content   = input('post.content');
        $new_type  = input('post.new_type');
        $admin_id  = session('admin_id');
        $data = [
            'title'        => $title,
            'content'      => $content,
            'new_type'     => $new_type,
            'admin_id'     => $admin_id,
            'addtime'     => time()
        ]; 
        $ret = db('new')->insert($data);
        if($ret ==false)
        {
            $this->error("添加新闻失败！");
        }
        $this->success("添加新闻成功！",'/admin/news/new_list');
    }

    public function add_notice_action(){
        $title     = input('post.title');
        $content   = input('post.content');
        $data = [
            'title'        => $title,
            'content'      => $content,
            'addtime'     => time()
        ]; 
        $ret = db('notice')->insert($data);
        if($ret ==false)
        {
            $this->error("添加公告失败！");
        }
        $this->success("添加公告成功！",'/admin/notice/notice_list');
    }

    public function modify_notice(){
        $id = $_GET['id'];
        $notice_info = db('notice')->where("id = {$id}")->find();
        $this->assign('notice_info',$notice_info);
        $this->assign('pick',5);
        return view('modify_notice');
    }

    public function modify_notice_action(){
        $id        = input('post.id');
        $title     = input('post.title');
        $content   = input('post.content');
        $data = [
            'title'        => $title,
            'content'      => $content,
            'addtime'     => time()
        ]; 
        $ret = db('notice')->where("id={$id}")->update($data);
        if($ret ==false)
        {
            $this->error("修改公告失败！");
        }
        $this->success("修改公告成功！",'/admin/notice/notice_list');
    }
}
